#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;

bool a[31][31][31];
int n;
int k,m;

int curK;
int cnt[1<<20];
int was;

vector<pair<int,int> > v[31];

bool dfs(int cur)
{
	if (cur==1)
		return true;

	was|=(1<<cur);

	for (int I=0,cnt=v[cur].size();I<cnt;I++)
	{
		int i=v[cur][I].first;
		int j=v[cur][I].second;

		if ((was&(1<<i))==0 && (curK&(1<<j)))
		{
			if (dfs(i))
				return true;
		}
	}
	return false;
}

int main()
{
#ifdef GOGOGO
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif

	scanf("%d%d%d",&k,&n,&m);
	for (int i=0;i<m;i++)
	{
		int f,l,j;
		scanf("%d%d%d",&f,&l,&j);
		a[f][l][j]=true;
		a[l][f][j]=true;
	}

	for (int i=0;i<n;i++)
		for (int j=0;j<n;j++)
			for (int l=0;l<k;l++)
				if (a[i][j][l])
					v[i].push_back(make_pair(j,l));

	int res_cnt=k;
	int res=(1<<k)-1;
	for (int i=1;i<(1<<k);++i)
	{
		cnt[i]=cnt[i&(i-1)]+1;

		if (cnt[i]>=res_cnt)
			continue;

		curK=i;
		was=0;
		if (dfs(0))
		{
			res=i;
			res_cnt=cnt[i];
		}
	}

	cout<<res_cnt<<endl;
	for (int i=0;i<k;i++)
		if (res&(1<<i))
			cout<<i<<" ";
	cout<<endl;
	return 0;
}